package com.tensquare.friend.controller;

import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/friend")
@CrossOrigin
public class FriendController {

    @Autowired
    private FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "/like/{friendid}/{type}",method = RequestMethod.PUT)
    public Result like(@PathVariable String friendid,@PathVariable String type){

        //解析token
        Claims claims = (Claims) request.getAttribute("user_claims");

        if(claims==null){
            return new Result(StatusCode.ACCESSERROR,false,"权限不足");
        }

        String userid = "";
        userid = claims.getId();
        if("1".equals(type)){ //添加好友
            int num = friendService.addFriend(userid, friendid);
            if(num==0){
                return new Result(StatusCode.REPERROR,false,"已经添加过此好友了");
            }
        }else{  //添加黑名单
            friendService.addNoFriend(userid,friendid);
        }

        return new Result(StatusCode.OK, true, "操作成功");
    }

    /**
          * 删除好友
          * @param friendid
          * @return
          */
    @RequestMapping(value="/{friendid}",method=RequestMethod.DELETE)
    public Result remove(@PathVariable String friendid){
        //解析token
        Claims claims = (Claims) request.getAttribute("user_claims");

        if(claims==null){
            return new Result(StatusCode.ACCESSERROR,false,"权限不足");
        }

        String userid = "";
        userid = claims.getId();

        friendService.deleteFriend(userid,friendid);

        return new Result(StatusCode.OK, true, "删除成功");
    }
}
